package emp.servlet.EmplayeeServlet;

import emp.dao.DepartmentDao;
import emp.dao.EducationDao;
import emp.dao.EmployeeDao;
import emp.entity.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 叶枫
 * @date 2024/12/18.
 */

@WebServlet("/employee")
public class AddServlet extends HttpServlet {
    private EmployeeDao employeeDao;
    private DepartmentDao departmentDao;
    private EducationDao educationDao;

    @Override
    public void init() throws ServletException {
        employeeDao = new EmployeeDao();
        departmentDao = new DepartmentDao();
        educationDao = new EducationDao();
    }
    public void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
        String action = request.getParameter("action");

        if ("list".equals(action)){
            list(request, response);
        }
        if ("getById".equals(action)){
        }
        if ("ls".equals(action)){
            listEducAndDept(request, response);
        }
    }
    public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
        String action = request.getParameter("action");
        if ("list".equals(action)){
            list(request, response);
        }
        if ("v".equals(action)){
            listEducAndDept(request, response);
        }
        if ("delete".equals(action)){
        }
        if ("update".equals(action)){
        }
        if ("add".equals(action)){
            add(request, response);
        }
    }
    public void list(HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {

        String empId = request.getParameter("empId");
        String empName = request.getParameter("empName");
        String edu = request.getParameter("edu");
        String hometown = request.getParameter("hometown");
        String pageNumber = request.getParameter("pageNumber");
        String pageSize = request.getParameter("pageSize");
        SearchCriteria searchCriteria = new SearchCriteria();
        searchCriteria.setPageNumber(Integer.parseInt(pageNumber));
        searchCriteria.setPageSize(Integer.parseInt(pageSize));
        if (empId != null){
            searchCriteria.setEducation(edu);
        }
        if (hometown != null){
            searchCriteria.setHometown(hometown);
        }
      if (empName != null){
          searchCriteria.setName(empName);
      }
      if (empId != null){
          searchCriteria.setJobNumber(empId);
      }
        PageInfo<Employee> employeePageInfo = employeeDao.listByPageSearch(searchCriteria);
        Integer count = employeeDao.getCount();
        employeePageInfo.setTotalPages(count);
        request.setAttribute("employeeList", employeePageInfo);
        request.getRequestDispatcher("/employee.jsp").forward(request, response);
    }
    public void add(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        String empName = request.getParameter("empName");
        String gender = request.getParameter("gender");
        String age = request.getParameter("age");
        String education = request.getParameter("edu");
        String salary = request.getParameter("salary");
        String address = request.getParameter("address");
        String hometown = request.getParameter("hometown");

        String phone = request.getParameter("phone");
        String departmentId = request.getParameter("dept");
        Employee employee = new Employee();
        employee.setEmpName(empName);
        employee.setGender(Integer.valueOf(gender));
        employee.setAge(Integer.parseInt(age));
        Education education1 = new Education();
        education1.setEduId(Integer.parseInt(education));
        employee.setEducation(education1);
        employee.setSalary(Double.parseDouble(salary));
        employee.setAddress(address);
        employee.setHometown(hometown);
        employee.setPhone(phone);
        employee.setDeptId(Integer.parseInt(departmentId));
        employeeDao.add(employee);

        response.sendRedirect("/employee?action=list&pageNumber=1&pageSize=10");
    }
    public void listEducAndDept(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {

        List<Education> educations = educationDao.list();
        List<Department> departments = departmentDao.list();
        request.setAttribute("eduList", educations);
        request.setAttribute("deptList", departments);
        request.getRequestDispatcher("/addEmployee.jsp").forward(request, response);

    }


    }


